package com.restaurant.application.accumulate;

import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.restaurant.application.accumulate.support.AbstractAccumulate;
import com.restaurant.application.accumulate.support.AccumulateData;
import com.restaurant.application.customer.Customer;
import com.restaurant.application.customer.support.CustomerData;
import com.restaurant.application.indexes.accumulatelist.mysql.AccumulateListTableBean;
import com.restaurant.application.indexes.accumulatelist.mysql.AccumulateListTableImpl;

import java.util.Objects;

public class AccumulateImpl extends AbstractAccumulate {
    @Override
    public View getFullView() {
        return null;
    }

    @Override
    public View getViewLead(ViewSpec viewSpec) {
        return null;
    }

    @Override
    public void init(AccumulateData data) {
        data.withCreateTime(Objects.isNull(data.getCreateTime()) ? DateTimeUtils.getCurrentMillisecond() : data.getCreateTime());
        if (Objects.isNull(data.getCustomerUID())) {
            data.withCustomerUID(getGate());
        }

        setData(data);
        insertBean();

    }

    private void insertBean() {
        CustomerData data = getUnit(Customer.class, getData().getCustomerUID()).findData();
        AccumulateListTableBean bean = new AccumulateListTableBean().withAccumulate(getId())
                .withType(getData().getType()).withPoints(getData().getAccumulate()).withAction(getData().getAction())
                .withCustomerUID(getData().getCustomerUID()).withCustomerName(data.getNickName()).withCustomerId(data.getId())
                .withCreateTime(getData().getCreateTime()).withPhone(data.getPhone());
        getIndex(AccumulateListTableImpl.class).insert(bean);

    }
}
